package com.wectrls.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author chenjian
 * @Package
 * @Description: prePostEnabled = true,//启用Spring Security前/后注释,@Secured注释用于指定方法上的角色列表,用户至少具有一个指定的角色，则用户能访问该方法:@Secured({ "ROLE_VIEWER", "ROLE_EDITOR" })
 * securedEnabled = true,//启用@Secured注释,
 * jsr250Enabled = true//使用@RoleAllowed注释,@RoleAllowed注释是JSR-250对@Secured注释的等效注释。@RolesAllowed({ "ROLE_VIEWER", "ROLE_EDITOR" })
 * @date 2019/1/12 22:01
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
        prePostEnabled = true,//启用Spring Security前/后注释,@Secured注释用于指定方法上的角色列表,用户至少具有一个指定的角色，则用户能访问该方法:@Secured({ "ROLE_VIEWER", "ROLE_EDITOR" })
        securedEnabled = true,//启用@Secured注释,
        jsr250Enabled = true//使用@RoleAllowed注释,@RoleAllowed注释是JSR-250对@Secured注释的等效注释。@RolesAllowed({ "ROLE_VIEWER", "ROLE_EDITOR" })
)
public class WebMvcSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // auth.userDetailsService(yzzhUserServiceImpl).passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /*public static void main(String[] args) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        String p ="$2a$10$eBpx0pI6ruEfY3fY6pI96OEwwSN80I3d7d91z4bLxH73BKL4PNbIC";
        boolean matches = passwordEncoder.matches("123456", p);
        System.out.println(matches);

    }*/


}
